package com.hust.bridgecheck.realm;

import com.hust.bridgecheck.entity.User;
import com.hust.bridgecheck.entity.UserGroup;
import com.hust.bridgecheck.service.UserManagement.GroupService;
import com.hust.bridgecheck.service.UserManagement.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

public class MyShiroRealm extends AuthorizingRealm {
    @Autowired
    private UserService userService;
    @Autowired
    private GroupService groupService;



    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        System.out.println("权限配置-->MyShiroRealm.doGetAuthorizationInfo()");
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        User userInfo  = (User)principals.getPrimaryPrincipal();
        UserGroup userGroup = groupService.getGroupByID(userInfo.getGroupID());
        if(userGroup==null)
            return null;
        if(userInfo.getUserAccount().equals("admin")){
            authorizationInfo.addStringPermission("root");
        }if (userGroup.getIsAlarm()==1){
            authorizationInfo.addStringPermission("Alarm");
        }if(userGroup.getIsAnalysis()==1){
            authorizationInfo.addStringPermission("Analysis");
        }if(userGroup.getIsNotice()==1){
            authorizationInfo.addStringPermission("Notice");
        }if(userGroup.getIsThreshold()==1){
            authorizationInfo.addStringPermission("Threshold");
        }if(userGroup.getIsUpload()==1){
            authorizationInfo.addStringPermission("Upload");
        }
        /*for(SysRole role:userInfo.getRoleList()){
            authorizationInfo.addRole(role.getRole());
            for(SysPermission p:role.getPermissions()){

            }
        }*/
        return authorizationInfo;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //System.out.println("MyShiroRealm.doGetAuthenticationInfo()");
        //获取用户的输入的账号.
        String username = (String)token.getPrincipal();
        System.out.println(token.getCredentials());
        //通过username从数据库中查找 User对象，如果找到，没找到.
        //实际项目中，这里可以根据实际情况做缓存，如果不做，Shiro自己也是有时间间隔机制，2分钟内不会重复执行该方法
        User userInfo = userService.getUserByUserAccount(username);
        System.out.println("----->>userInfo="+userInfo);
        if(userInfo == null){
            return null;
        }
        UserGroup group = groupService.getGroupByID(userInfo.getGroupID());
        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
                userInfo, //用户
                userInfo.getPassWd(), //密码
                getName()  //realm name
        );
        Session session = SecurityUtils.getSubject().getSession();
        session.setAttribute("user", userInfo);
        session.setAttribute("groupName",group.getGroupName());
        return authenticationInfo;
    }
}
